package zdream.pmfield.stage.buff.seat;

import zdream.pmfield.stage.director.AttackContext;
import zdream.pmfield.stage.director.Roll;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.director.Hooks;
import zdream.pmfield.stage.object.Seat;
import zdream.pmfield.stage.object.StageMove;

/**
 * @author Zdream
 * @date 2023-12-01
 * @since 0.0.1
 */
public class SpaceBuff extends SeatStageBuff {
	public static final String CATEGORY = Seat.BUFF_SPACE;

	/**
	 * 计算精确度衰减, 是从攻击者出发位置 (不包含) 到目标位置 (包含) 的所有 {@link Seat},
	 * 每经过一个 {@link Seat}, 精确度下降这个 {@link Seat} 拥有的 {@link SpaceBuff}
	 * 的 accuracyAttenuation.
	 */
	public int accuracyAttenuation;

	public SpaceBuff(Seat seat) {
		super(seat);
		this.accuracyAttenuation = seat.stage.accuracyAttenuationForDistance;
	}

	@Override
	public void init() {
		onHook(HookType.BEFORE_ATTACK_ROLL_CHECK)
				.handle(this::beforeRollCheck)
				.buildAndSave();
	}

	private void beforeRollCheck(Hooks.HookContext rawContext) {
		Hooks.AttackHookContext context = rawContext.as();
		final AttackContext attackContext = context.attackContext;
		if (attackContext.action.targetSeat == null) {
			return;
		}

		if (target.global || target.x == attackContext.attacker.seat.x) {
			return;
		}
		final StageMove currentMove = attackContext.currentMove;
		if (!currentMove.target.route.accuracyPenalty) {
			return;
		}

		for (Roll roll : attackContext.rolls) {
			roll.fixedBaseValue -= this.accuracyAttenuation;
		}
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	@Override
	public String toString() {
		return "空间:" + accuracyAttenuation;
	}
}
